package com.hitqz.robot.biz.flow.component.patrol;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Slf4j
@LiteflowComponent(value = "GoToFactoryPosition", name = "[巡检]前往厂房区域")
@AllArgsConstructor
public class GoToFactoryPosition extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;


    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        log.warn("GoToFactoryPosition endParams==>:\n{}", JSONObject.toJSONString(endParams));
        Map<String, Object> params = endParams.getModalData();
        String inspectFactoryArea = params.get("inspectFactoryArea") != null ? params.get("inspectFactoryArea").toString() : null;
        String factoryAreaPosIds = params.get("factoryAreaPosIds") != null ? params.get("factoryAreaPosIds").toString() : "";
        if ("1".equals(inspectFactoryArea)) {//需要巡检厂房区域
            List<String> posIds = StringUtils.hasText(factoryAreaPosIds) ?
                    Arrays.asList(factoryAreaPosIds.split(",")).stream().filter(StringUtils::hasText).toList()
                    : new ArrayList<>();
            log.info("开始进行厂房巡检==>");
            for (String posId : posIds) {
                this.go2TargetPosition(Integer.parseInt(posId));
            }
        }
    }

    private void go2TargetPosition(Integer posId) throws Exception {
        RobotMapPos robotMapPos = robotMapPosService.getById(posId);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        if (Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached()) {
            log.info("已在目标点{},跳过！", robotMapPos.getCode());
        } else {
            log.info("前往厂房点位 {}", robotMapPos.getCode());
            //调用规划路径算法的前往站点
            dispatchService.go2TargetPosition(robotMapPos, false);
            TimeUnit.SECONDS.sleep(1);
            iRobotPosDto = robotDriverService.getWebPos();
            while ( !(Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached())) {
                iRobotPosDto = robotDriverService.getWebPos();
                TimeUnit.SECONDS.sleep(1);
                //外部中断标志
                LiteFlowUtil.checkInterruptFlag();
            }
            log.info("机器人到达厂房目标位置了！");
        }
        log.info("到达厂房点位坐标 {}---- x: {}, y: {}, yaw {}", iRobotPosDto.getCurrentStation(),
                iRobotPosDto.getX(), iRobotPosDto.getY(), iRobotPosDto.getAngle());
    }

}